package com.yiliao.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yiliao.control.UploadFileControl;
import com.yiliao.service.ICommService;
import com.yiliao.service.impl.ICommServiceImpl;

public class PoolThread extends Thread {

	Logger logger = LoggerFactory.getLogger(getClass());
	
	Integer userId;
	String channelName;
	private static ICommService iCommServiceImpl = null;
	private static UploadFileControl uploadFileControl = null;
	static {
		iCommServiceImpl = (ICommService) SpringConfig.getInstance().getBean("iCommServiceImpl");
		uploadFileControl = (UploadFileControl) SpringConfig.getInstance().getBean("uploadFileControl");
	}
	public PoolThread(int userId, String channelName) {
		super();
		this.userId = userId;
		this.channelName = channelName;
	}
	
	public String getFileName(String suffix) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		String fileName = sdf.format(new Date());
		//随机数
		String number = Math.round((Math.random() * 10000)) + "";
		fileName = fileName+number;
		//获取文件格式
        suffix = suffix.indexOf(".") != -1 ? suffix.substring(suffix.lastIndexOf("."), suffix.length()) : null;
        //新的文件名
        fileName = new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+fileName+suffix;
		return fileName;
	}
	@Override
	public void run() {

		ArrayList<Map> arrayList = new ArrayList<Map>();
		try {
			String channel_pac_dow_url = SystemConfig.getValue("channel_pac_dow_url");
			String channel_pac_adress = SystemConfig.getValue("channel_pac_adress");
			
			StringBuffer sb = new StringBuffer();
			sb.append("java -jar "+channel_pac_adress+"packer-ng-2.0.1.jar  generate --channels=");
			sb.append(userId+" --output="+channel_pac_dow_url+userId+" "+channel_pac_adress+"app.apk");
			logger.info("执行linux代码:{}",sb.toString());
            int waitFor = Runtime.getRuntime().exec(sb.toString()).waitFor();
            logger.info("linux语句执行结束:{}",waitFor);
            
            //文件下载url
			String fileUrl=channel_pac_dow_url+userId+"/app-"+userId+".apk";
			 logger.info("渠道包文件地址:{}",fileUrl);
			//OSS下载开始
			 File uploadFile =new File(fileUrl);
//			File uploadFile =new File("D:/speed/img/app.apk");
			//String newName = getFileName("app-"+userId+".apk");// 重新生成文件名
			String newName = userId+".apk";
			String filePath = uploadFileControl.ossUpload(newName, uploadFile);
			String url=filePath+newName;
			logger.info("渠道包OSS文件地址:{}",url);
			//新增渠道地址
            HashMap<String,Object> hashMap = new HashMap<String, Object>();
            hashMap.put("t_user_id", userId);
            hashMap.put("t_channel_pac_name", channelName);
            hashMap.put("t_dow_url", url);
            hashMap.put("t_create_time", DateUtils.format(new Date(), DateUtils.FullDatePattern));
            iCommServiceImpl.executeSql("insert into t_channel_pac_dow_log (t_user_id,t_channel_pac_name,t_dow_url,t_create_time) values (?,?,?,now()) ", 
            		userId,channelName,url);
			arrayList.add(hashMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
	}


}
